package com.ruoyi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.domain.common.UserHolder;
import com.ruoyi.domain.entity.ForumPost;
import com.ruoyi.domain.entity.ForumPostDto;
import com.ruoyi.mapper.ForumPostMapper;
import com.ruoyi.service.ForumPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 论坛Service业务层处理
 * 
 * @author lsx
 * @date 2025-01-07
 */
@Service
public class ForumPostServiceImpl extends ServiceImpl<ForumPostMapper, ForumPost> implements ForumPostService
{
    @Autowired
    private ForumPostMapper forumPostMapper;
    @Override
    public ForumPostDto getInfoById(Integer id) {
        return forumPostMapper.selectForumPostById(id);
    }

    @Override
    public boolean publishPost(ForumPost forumPost) {
        return forumPostMapper.insertForumPost(forumPost);
    }

    @Override
    public void likePost(Integer postId) {
        Integer userId = UserHolder.getUserId();
        int count=forumPostMapper.getLikeCount(userId,postId);
        if (count==0){
            forumPostMapper.changePraise(postId, 1);
            forumPostMapper.likePost(userId,postId);
        }else {
            forumPostMapper.changePraise(postId, -1);
            forumPostMapper.cancelLikePost(userId,postId);
        }
    }

    @Override
    public List<Integer> getLikeUserIds(Integer forumId) {
        return forumPostMapper.getLikeUserIds(forumId);
    }

    @Override
    public List<ForumPostDto> getTopLikedStockPosts(Integer stockId, Integer limit) {
        return forumPostMapper.getTopLikedStockPosts(stockId,limit);
    }
}
